﻿@{
    ViewBag.Title = "工单管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                <option value="">所有车间</option>
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" placeholder="请输入订单号" value="" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" placeholder="请输入工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderDate">工单日期：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeDate" placeholder="请选择工单日期" value="@(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd") + " 至 " + DateTime.Today.ToString("yyyy-MM-dd"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button id="btnBatchSearch" class="layui-btn layui-btn-warm layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>批量查询
                        </button>
                        <a id="btnDownload" class="layui-btn layui-btn-normal layuiadmin-btn-list" href=":;" target="_blank">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form','laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/Report/QueryWorkOrders'
                    , method: "post"
                    , where: { workshopCode: "", orderNo: "", workOrderNo: "", status: null, startDate: "@DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd")", endDate: "@DateTime.Today.ToString("yyyy-MM-dd")" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 200, fixed: "left" }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150, fixed: "left" }
                        , { field: 'WorkshopName', title: '生产车间', minWidth: 120 }
                        , { field: 'ProductCode', title: '成品料号', minWidth: 200 }
                        , { field: 'ProductName', title: '成品名称', minWidth: 250 }
                        , { field: 'StartDateStr', title: '开工日期', minWidth: 150 }
                        , { field: 'EndDateStr', title: '完工日期', minWidth: 150 }
                        , { field: 'Count', title: '数量', minWidth: 70 }
                        , { field: 'CellQuantity', title: '电池片数', minWidth: 100 }
                        , { field: 'CellLevel', title: '电池等级', minWidth: 100 }
                        , { field: 'CellModel', title: '电池型号', minWidth: 150 }
                        , {
                            title: '电池效率', minWidth: 100, templet: function (d) {
                                return d.CellEfficiencyMin == null ? '' : (d.CellEfficiencyMin + "-" + d.CellEfficiencyMax);
                            }
                        }
                        , {
                            title: '产出功率', minWidth: 100, templet: function (d) {
                                var powerArray = new Array();
                                for (var i = 0; i < d.OutputPowers.length; i++) {
                                    powerArray.push(d.OutputPowers[i].OutputPower);
                                }
                                return powerArray.join('、');
                            }
                        }
                        , {
                            title: '铭牌型号', minWidth: 200, templet: function (d) {
                                var powerArray = new Array();
                                for (var i = 0; i < d.BinningPowers.length; i++) {
                                    powerArray.push(d.BinningPowers[i].NameplateModel);
                                }
                                return powerArray.join('、');
                            } }
                        , { field: 'CustomerCode', title: '客户代码', minWidth: 100 }
                        , { field: 'CustomerName', title: '客户名称', minWidth: 150 }
                        , { field: 'DistributeQuantity', title: '分片数量', minWidth: 100 }
                        , { field: 'LaminatedQuantity', title: '叠层数量', minWidth: 150 }
                        , { field: 'PackQuantity', title: '打包数量', minWidth: 100 }
                        , { field: 'InboundQuantity', title: '入库数量', minWidth: 150 }
                        , { field: 'InboundRate', title: '入库达成率', minWidth: 150 }
                        , { field: 'ReviewTimeStr', title: '审核时间', minWidth: 150 }
                        , { field: 'WorkOrderTimeStr', title: '同步时间', minWidth: 150 }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.workshopCode = $("#selWorkshops").val().trim();
                    options.where.orderNo = $("#txtOrderNo").val().trim();
                    options.where.workOrderNo = $("#txtWorkOrderNo").val().trim();
                    var rangeTime = $("#txtRangeDate").val().trim() == '' ? new Array() : $("#txtRangeDate").val().trim().split(' 至 ');
                    options.where.startDate = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endDate = rangeTime.length <= 0 ? null : rangeTime[1];

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                $("#btnBatchSearch").click(function () {
                    layer.prompt({
                        formType: 2,
                        title: '请输入工单号',
                        maxlength: 100000,
                        area: ['800px', '350px'] //自定义文本域宽高
                    }, function (value, index, elem) {
                        var workorderNos = value.split('\n');
                        options.where.workOrderNo = workorderNos.join(',');
                        table.reload("table", options);
                        layer.close(index);
                    });
                });

                $("#btnDownload").click(function () {
                    if (options.where.orderNo.trim() == '' && options.where.workOrderNo.trim() == '') {
                        layer.msg("订单号和工单号必须填写一个", { icon: 5 });
                        return false;
                    }
                    var paras = new Array();
                    for (var item in options.where) {
                        paras.push(item + "=" + options.where[item]);
                    }
                    $(this).attr("href", "/Report/ExportWorkOrders?" + paras.join("&"));
                });
            });
        }
    </script>
}
